package com.example.lprxjava;

import android.util.Log;
import android.view.View;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Author by lp,on 2021/7/20/020,14:27.
 * Rxjava 创建
 */
public class CreateFragement extends BaseFragment {

    private static final String TAG = "CreateFragement";

    @Override
    String getTAG() {
        return TAG;
    }

    @Override
    String getTitle() {
        return "RxJava 创建 操作符";
    }

    @Override
    int getFragmentLayoutId() {
        return R.layout.create_frqagment;
    }

    @Override
    void onLoad(View view) {
        view.findViewById(R.id.btn_create).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                create();
            }
        });
        view.findViewById(R.id.btn_just).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                just();
            }
        });
        view.findViewById(R.id.btn_from_array).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fromArray();
            }
        });
        view.findViewById(R.id.btn_fromIterable).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fromIterable();
            }
        });
        view.findViewById(R.id.btn_interval).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                interval();
            }
        });
    }

    private void interval() {
        Observable<Long> interval = Observable.interval(2, 1, TimeUnit.SECONDS);
        interval.subscribe(consumer);
    }

    //集合方式 使用fromIterable()，遍历集合，发送每个item。相当于多次回调onNext()方法，每次传入一个item。
    private void fromIterable() {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("fromIterable方式创建");
        arrayList.add("fromIterable方式创建2");
        arrayList.add("fromIterable方式创建3");
        //无个数限制
        Observable.fromIterable(arrayList).subscribe(consumer);
    }

    private void fromArray() {
        //无个数限制
        Observable.fromArray("fromArray方式创建", "222", "333").subscribe(consumer);
    }

    //将为你创建一个Observable并自动为你调用onNext( )发射数据。通过just( )方式 直接触发onNext()，just中传递的参数将直接在Observer的onNext()方法中接收到。
    private void just() {
        //限制 10个
        Observable.just("just方式创建", "just1", "just2").compose(RxLifecycle.<String>bindRxLifecycle(this)).subscribe(consumer);
    }

    private void create() {
        observable = Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> e) throws Exception {
                Log.i(TAG, "current Thread: " + Thread.currentThread().getName());
                e.onNext("create方式创建...");
            }
        });
        observable.subscribe(observer);
        observable.subscribe(consumer);
    }

    //消费者，更加简洁接受异步消息
    private Consumer<Object> consumer = new Consumer<Object>() {
        @Override
        public void accept(Object o) throws Exception {
            Log.i(TAG, "accept: " + o);
            stringBuffer.append(o.toString() + "\r\n");
            showText();
        }
    };


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(TAG, "onDestroyView: ");

    }
}
